package com.chandon.T05_timeApi.T01_temporal;

import java.time.*;
import java.time.temporal.ChronoUnit;
import java.util.Date;

/**
 * Indstant时间戳类用于代替Date类
 *
 * @author Dong
 * @version 1.0
 * @date 2021-04-22 20:02
 */

public class T01_InstantTest {
    public static void main(String[] args) {

//        createInstant();

        Instant now = Instant.now();

        // 不同时区/时间偏移 时间戳
        ZonedDateTime zonedDateTime1 = now.atZone(ZoneId.of("Asia/Shanghai"));
        ZonedDateTime zonedDateTime2 = now.atZone(ZoneOffset.ofHours(+8));
        ZonedDateTime zonedDateTime3 = now.atZone(ZoneOffset.ofHoursMinutes(+8,10));

        OffsetDateTime offsetDateTime = now.atOffset(ZoneOffset.ofHours(1));
        OffsetDateTime offsetDateTime1 = now.atOffset(ZoneOffset.ofHours(-1));




        // 减去/增加 对应时间数量的时间戳
        // ChronoUnit表示一段时间间隔的度量，接口TemporalUnit
        Instant minTime = now.minus(1, ChronoUnit.MINUTES);
        Instant plusTime = now.plus(1, ChronoUnit.MINUTES);

        System.out.println(now);
        System.out.println(zonedDateTime1);
        System.out.println(zonedDateTime2);
        System.out.println(zonedDateTime3);
        System.out.println(offsetDateTime);
        System.out.println(offsetDateTime1);
/*        System.out.println(minTime);
        System.out.println(plusTime);*/

    }

    private static void createInstant() {
        //获取当前时间的时间戳  2021-04-22T12:05:21.751Z
        Instant nowInstant = Instant.now();
        System.out.println(nowInstant);

        // Date毫秒转Instant 2021-04-25T10:50:34.991Z
        Instant oldDateInstant = Instant.ofEpochMilli(new Date().getTime());
        System.out.println(oldDateInstant);

        // 解析字符串为时间戳,可支持到纳秒  2021-04-25T08:57:32.299468169Z
        Instant parse = Instant.parse("2021-04-25T08:57:32.299468169Z");
        System.out.println(parse);

        // 秒数转时间戳  1970-01-08T22:11:31Z
        Instant secondInstant = Instant.ofEpochSecond(684691);
        System.out.println(secondInstant);
        // 毫秒数转时间戳  1987-06-04T22:46:38.411Z
        Instant milli = Instant.ofEpochMilli(549845198411L);
        System.out.println(milli);

        // 纳秒级调整  1970-01-01T01:40:00.010Z
        Instant ajustInstant = Instant.ofEpochSecond(3600, 1000000000L);
        Instant ajustedInstant = Instant.ofEpochSecond(6000, -1000000000L);
        System.out.println(ajustInstant);
        System.out.println(ajustedInstant);
    }

}
